import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    component: () => import('../views/LoginView.vue'),
    name: 'login',
  },
  {
    path: '/',
    component: () => import('../App.vue'), // 主布局组件
    meta: { requiresAuth: true }, // 需要认证的父路由
    children: [
      // 重定向配置放在子路由中
      {
        path: '',
        redirect: '/dash/sale'
      },
      // 所有需要侧边栏的路由
      {
        path: '/dash/behavior',
        component: () => import('../views/dash/BehaviorView.vue'),
        name: 'behavior',
      },
      {
        path: '/dash/sale',
        component: () => import('../views/dash/SaleView.vue'),
        name: 'sale',
      },
      // 其他需要侧边栏的路由...
      // 保持原有结构，只需移动到children中
      {
        path: '/dash/suggestion',
        component: () => import('../views/dash/SuggestionView.vue'),
        name: 'suggestion',
      },
      {
        path: '/goods/process',
        component: () => import('../views/goods/ProcessView.vue'),
        name: 'process',
      },
      {
        path: '/goods/sort',
        component: () => import('../views/goods/SortView.vue'),
        name: 'sort',
      },
      {
        path: '/goods/violation',
        component: () => import('../views/goods/ViolationView.vue'),
        name: 'violation',
      },
      {
        path: '/goods/information',
        component: () => import('../views/goods/InformationView.vue'),
        name: 'information',
      },
      {
        path: '/message/account',
        name: 'account',
        component: () => import('../views/message/AccountView.vue')
      },
      {
        path: '/orders/handle',
        component: () => import('../views/orders/HandleView.vue'),
        name: 'handle',
      },
      {
        path: '/sem/activity',
        component: () => import('../views/sem/ActivityView.vue'),
        name: 'activity',
      }, 
      {
        path: '/sem/advertise',
        component: () => import('../views/sem/AdvertiseView.vue'),
        name: 'advertise',
      }, 
      {
        path: '/sem/customer',
        component: () => import('../views/sem/CustomerView.vue'),
        name: 'customer',
      }, 
      {
        path: '/transaction/dispute',
        component: () => import('../views/transaction/DisputeView.vue'),
        name: 'dispute',
      },
      // 测试组件路由建议移除或添加权限控制
      {
        path: '/topbar',
        component: () => import('../components/TopBar.vue'),
        name: 'topbar',
        meta: { requiresAuth: true }
      },
      {
        path: '/side',
        component: () => import('../components/SidebarMenu.vue'),
        name: 'side',
        meta: { requiresAuth: true }
      },
    ]
  }
];

const router = new VueRouter({
  mode: 'history',
  routes
});

router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; // 统一获取登录状态

  // 登录页特殊处理：已登录时跳转到首页
  if (to.path === '/login') {
    isLoggedIn ? next('/') : next(); // 直接返回，避免逻辑嵌套
    return;
  }

  // 检查路由是否需要认证（meta.requiresAuth 为 true）
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isLoggedIn) {
      // 未登录时重定向到登录页，并记录目标路径
      next({ path: '/login', query: { redirect: to.fullPath } });
    } else {
      next(); // 已登录，允许访问
    }
  } else {
    next(); // 不需要认证的路由直接通过
  }
});

export default router;